﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.Sql;
using System.Data.SqlClient;
using System.Data.SqlTypes;

namespace WindowsFormsApplication1
{
    public partial class Supplier : Form
    {
        public Supplier()
        {
            InitializeComponent();
        }
        private void textBox1_MouseClick(object sender, MouseEventArgs e)
        {
            try
            {
                String getSupNo = "SELECT ISNULL(MAX(suppcode),0) FROM supplier";
                SqlDataReader mxdr = DbConnet.ReadTable(getSupNo);

                if (mxdr.Read())
                {
                    String mx = mxdr[0].ToString();
                    mx = mx.Substring(mx.IndexOf("S") + 1);
                    int a = 0;
                    a = (int.Parse(mx)+1);
                    mx = "S00" + a;

                    txtsupCode.Text = mx;
                }
                mxdr.Close();
            }
            catch (Exception exmx)
            {
            }

        }

        private void Supplier_Load(object sender, EventArgs e)
        {
            DbConnet.connect(new Log().getConstr());
            
        }

        private void button1_Click(object sender, EventArgs e)
        {
            Boolean upd = false;
            Boolean ins = false;

            try
            {
                String updsup = "UPDATE supplier SET suppname = '" + txtSupName.Text.Trim() + "', supptel = '" + txtSupTel.Text.Trim() + "', suppaddr = '" + TxtSupAdd.Text.Trim() + "' , updateby = '" + new Midval().getUser() + "', updateddate = '" + DateTime.Now + "' WHERE Suppcode = '"+txtsupCode.Text.Trim()+"'";

                String insSup = "INSERT INTO supplier (Suppcode,suppname,supptel,suppaddr,createdby,createddate) " +
                                " VALUES ('" + txtsupCode.Text.Trim() + "','" + txtSupName.Text + "','" + txtSupTel.Text.Trim() + "','" + TxtSupAdd.Text.Trim() + "','"+new Midval().getUser()+"','" + DateTime.Now + "')";

                upd = DbConnet.AddEditDel(updsup);

                if (!upd)
                    ins = DbConnet.AddEditDel(insSup);

                if (upd || ins)
                {
                    MessageBox.Show(new Form() { TopMost = true }, "Supplier Details Added Update Successfully ", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    TxtSupAdd.Clear();
                    txtsupCode.Clear();
                    txtSupName.Clear();
                    txtSupTel.Clear();
                }

            }
            catch (Exception insup)
            {
            }
        }

        private void txtsupCode_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                try
                {
                    String getDetails = "SELECT Suppcode,suppname,supptel,suppaddr FROM supplier WHERE Suppcode = '" + txtsupCode.Text.Trim() + "'";
                    SqlDataReader ddr = DbConnet.ReadTable(getDetails);

                    if (ddr.Read())
                    {
                        txtsupCode.Text = ddr["Suppcode"].ToString();
                        txtSupName.Text = ddr["suppname"].ToString();
                        txtSupTel.Text = ddr["supptel"].ToString();
                        TxtSupAdd.Text = ddr["suppaddr"].ToString();
                    }
                    ddr.Close();
                }
                catch (Exception exgetd)
                {
                }
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            this.Hide();
        }
    }
}
